import http from './interface'
import config from '../config.js'
import util from '@/common/util.js';
/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 * 
 */
// 请求数据处理
function serialize(obj, prefix) {
    const str = []
    let p
    if (obj.length === 0) {
        //  str.push(encodeURIComponent(prefix) + '=1')  
    } else {
        for (p in obj) {
            if (obj.hasOwnProperty(p)) {
                const k = prefix ? prefix + '[' + p + ']' : p
                let v = obj[p]
                if (v instanceof Date) {
                    v = parseTime(v)
                }
                str.push((v !== null && typeof v === 'object') ? serialize(v, k) : encodeURIComponent(k) + '=' + encodeURIComponent(
                    v))
            }
        }
    }

    return str.join('&')
}
export const setPath = (pname) => {
	var path = config[pname];
	var obj = {
		apiPost: (url, data) => {
			//设置请求前拦截器
			http.interceptor.request = (config) => {
				let url = config.url
				// 对部分接口需要将token放到请求头中
				let reg = /insertStuTime/g
				if (reg.test(url)) {
					config.header = {
						"token": uni.getStorageSync('token'),
						"reqMethod": "2",
						"Content-Type": "application/json;charset=UTF-8"
					}
				}
				if (pname == "lessonUrl") {
					var header = url.substr(-13, 13);
					if (header == 'login/account') {
						config.header.Authorization = uni.getStorageSync('token')
					} else {
						config.header.Authorization = uni.getStorageSync('atoken')
					}
				} else if (pname == "serviceUrl") {
					config.header.Authorization = uni.getStorageSync('token')
				} else if (pname == 'noteUrl') {
					let reg = /upLoadFile/g
					if (reg.test(url)) {
						config.header = {
							"Content-Type": "multipart/form-data"
						}
					}
				} else {
					delete config.header.Authorization
				}
			}
			//设置请求结束后拦截器
			http.interceptor.response = (response) => {
				//判断返回状态 执行相应操作
				return response;
			}
			let dataurl = path + url

			if (data) {
				if (data.hasOwnProperty('pwd')) {
					data.pwd = util.getDES(String(data.pwd))
				} else if (data.hasOwnProperty('user_id')) {
					data.user_id = util.getDES(String(data.user_id))
				} else if (data.hasOwnProperty('userId')) {
					// 课表下面的请求用户ID不用加密
					let reg = /stuCourse/g
					if (!reg.test(url)) {
						data.userId = util.getDES(String(data.userId))
					}
				} else if (data.hasOwnProperty('tc_user_id')) {
					data.tc_user_id = util.getDES(String(data.tc_user_id))
				} else if (data.hasOwnProperty('student_id')) {
					data.student_id = util.getDES(String(data.student_id));
				} else if (data.hasOwnProperty('admin_user_id')) {
					data.admin_user_id = util.getDES(String(data.admin_user_id));
				} else if (data.hasOwnProperty('mechanism_id')) {
					data.mechanism_id = util.getDES(String(data.mechanism_id))
				} else if (data.hasOwnProperty('uids')) {
					//uids 是 用户id 字符串集合，需要先拆分为数组后逐个加密再转为字符串集合
					// var uids = data.uids.split(','); //用户ID集合数组
					// var uids_ary = []; //新的数组存储加密后ID集合数组
					// $.each(uids, function(i) {
					// 	uids_ary.push(util.getDES(String(uids[i]))) //加密
					// })
					// data.uids = uids_ary.join(',') //字符串集合
					data.uids = util.getDES(String(data.uids))
				}
			}

			let token = {
				'k_token': uni.getStorageSync('token') || uni.getStorageSync('_token'),
				'token': uni.getStorageSync('token') || uni.getStorageSync('_token')
			}
			// 自定课表创建在头部添加token这里不需要额外添加
			let reg = /insertStuTime/g
			if (!reg.test(url)) {
				data = Object.assign({}, data, token)
				data = serialize(data)
			}
			return http.request({
				url: dataurl,
				data
			})
		},
		apifile: (url, data) => {
			//设置请求结束后拦截器
			http.interceptor.response = (response) => {
				//判断返回状态 执行相应操作
				return response;
			}
			let dataurl = path + url

			let token = {
				'k_token': uni.getStorageSync('token') || uni.getStorageSync('_token'),
				'token': uni.getStorageSync('token') || uni.getStorageSync('_token')
			}
			data = Object.assign({}, data, token)
			return http.uploadfiles({
				url: dataurl,
				data
			})
		}
	}
	return obj;
}


// 默认全部导出  import api from '@/common/vmeitime-http/'
export default {
	setPath
}
